package cn.dubito.springcloud.controller;

import cn.dubito.springcloud.entity.Dept;
import cn.dubito.springcloud.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author liliang
 * @description:
 * @date 2018/5/19.
 */
@Slf4j
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    @Autowired
    private DiscoveryClient client;

    @PostMapping("/dept/add")
    public boolean add(@RequestBody Dept dept) {
        return deptService.add(dept);
    }

    @GetMapping("/dept/{deptNo}")
    public Dept get(@PathVariable("deptNo") Long deptNo) {
        return deptService.get(deptNo);
    }

    @GetMapping("/dept/list")
    public List<Dept> list() {
        return deptService.list();
    }

    @GetMapping("/dept/discovery")
    public Object discovery() {
        List<String> services = client.getServices();
        log.info("clientServices: {}", services);
        List<ServiceInstance> instances = client.getInstances("MICROSERVICECLOUD-DEPT");
        for (ServiceInstance s : instances) {
            log.info("serviceId:{},serviceHost:{},servicePort:{},serviceUri:{}",
                    s.getServiceId(), s.getHost(), s.getPort(), s.getUri());
        }
        return client;
    }
}
